/*
 * ItemDAOImpl.java
 *
 * Created on July 6, 2007, 3:15 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package com.abbh.libcommunity.dao.impl;
import com.abbh.libcommunity.dao.ItemDAO;
import com.abbh.libcommunity.dao.LCHibernateUtil;
import com.abbh.libcommunity.model.Item;
import com.abbh.libcommunity.model.UserBoughtItems;

import java.util.Date;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.Query;



/**
 *
 * @author Atef A. Ahmed
 */
public class ItemDAOImpl implements ItemDAO{
    
    Session s;
    Transaction t;
    Query q;
    List l;
    
    /* Creates a new instance of ItemDAOImpl */
    public ItemDAOImpl() {
       s=LCHibernateUtil.currentSession();
      t=s.beginTransaction();
        q=null;
        l=null;
    }
    
    public void save(Item item){
        s.save(item);
        t.commit();
    }
    public void update(Item item){
        s.update(item);
        t.commit();
    }
    
     public Item findByItemId(Integer itemId)  
     {
         Query q=s.createQuery("select p from Item p where p.id="+itemId);
         return (Item)q.uniqueResult();
     }
    
    
    public void delete(Item item){
        s.delete(item);
        t.commit();
    }
    
    public void sellItem(Item itemId){
         
        s.save(itemId);
        t.commit();

    }   
    public   List findBycreatedUser(String  name) {
        q = s.createQuery("select item from Item where Item.createdUser="+name);
        l = q.list();
        return l;
    }
    public   List findByCreationDate(Date date) {
        q = s.createQuery("select item from Item where Item.createdDate="+date);
        l = q.list();
        return l;
    }
    public   List findByDescription(String desc) {
        q = s.createQuery("select item from Item where Item.description="+desc);
        l = q.list();
        return l;
    }
    public   Item findById(Integer id) {
        q = s.createQuery("select item from Item where Item.id="+id);
         return (Item)q.uniqueResult();
        
    }    
    public   Item findByISBN(String isbn) {
        q = s.createQuery("select item from Item where Item.isbn="+isbn);
        return (Item)q.uniqueResult();
    }    
    public   List findByItemCategory(String category) {
        q = s.createQuery("select item from Item where Item.itemCategory="+category);
        l = q.list();
        return l;
    }
    public   List findByItemType(String type) {
        q = s.createQuery("select item from Item where Item.itemType="+type);
        l = q.list();
        return l;
    }    
    public   List findByLastModifiedDate(Date date) {
        q = s.createQuery("select item from Item where Item.lastModifiedDate="+date);
        l = q.list();
        return l;
    }
    public   List findByLastModifiedUser(String username) {
        q = s.createQuery("select item from Item where Item.lastModifiedUser="+username);
        l = q.list();
        return l;
    }   
    public   List findByPublisher(String publisher) {
        q = s.createQuery("select item from Item where Item.publisher="+publisher);
        l = q.list();
        return l;
    }
    public   List findByRating(String rating) {
        q = s.createQuery("select item from Item where Item.rating="+rating);
        l = q.list();
        return l;
    }
    public   Item findByTitle(String title){
        q = s.createQuery("select item from Item where Item.title="+title);
        return (Item)q.uniqueResult();
    }
    public   List findByYear(Integer year) {
        q = s.createQuery("select item from Item where Item.year="+year);
        l = q.list();
        return l;
    }


   
}
